package com.fary.api;

import cn.hutool.core.util.StrUtil;
import cn.hutool.extra.spring.SpringUtil;
import com.fary.common.ApiResponse;
import com.fary.dto.PaymentChannelDTO;
import com.fary.dto.PaymentInfoDTO;
import com.fary.entity.PaymentChannel;
import com.fary.entity.PaymentInfo;
import com.fary.service.OpenPaymentCoreService;
import com.fary.service.PaymentChannelService;
import com.fary.strategy.PayStrategy;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.math.BigDecimal;

@RestController
@RequestMapping("/api/v1")
@Slf4j
public class OpenPaymentCoreApi {

    @Autowired
    private OpenPaymentCoreService openPaymentCoreService;

    @Autowired
    private PaymentChannelService paymentChannelService;

    @PostMapping("testToPay")
    public ApiResponse<String> testToPay(@RequestBody(required = false) PaymentInfoDTO paymentInfoDTO) {
        return ApiResponse.success();
    }

    /**
     * 商家后台形式将支付的内容信息 传递给我们极简支付
     * 极简支付返回对应的支付令牌
     *
     * @return
     */
    @PostMapping("/createPayToken")
    public ApiResponse<String> createPayToken(@RequestBody PaymentInfoDTO paymentInfoDto) {
        // 1.验证参数
        String merchantId = paymentInfoDto.getMerchantId();
        if (StrUtil.isEmpty(merchantId)) {
            return ApiResponse.error("merchantId is null");
        }
        BigDecimal paymentAmount = paymentInfoDto.getPayAmount();
        if (paymentAmount == null) {
            return ApiResponse.error("paymentAmount is null");
        }
        // 2.新增支付数据
        // 3.返回对应支付令牌给我们 商家
        String payToken = openPaymentCoreService.createPayToken(paymentInfoDto);
        log.info("<payToken：{}>", payToken);
        return ApiResponse.success(payToken);
    }

    /**
     * 参数1：支付渠道
     * 参数2：支付令牌 明文支付的参数
     * 联合登录（qq联合登录、微信联合登录、其他联合）
     * 对接第三方支付系统（支付宝、银联支付、平安支付以form表单将数据提交到
     * 第三方支付系统的）
     */
    @PostMapping("toPay")
    public ApiResponse<String> toPay(@RequestBody PaymentChannelDTO paymentChannelDto) {
        // 1.验证参数
        String channelId = paymentChannelDto.getChannelId();
        if (StrUtil.isEmpty(channelId)) {
            return ApiResponse.error("channelId is null");
        }
        String payToken = paymentChannelDto.getPayToken();
        if (StrUtil.isEmpty(payToken)) {
            return ApiResponse.error("payToken is null");
        }
        // 2.根据支付令牌 获取支付的参数信息
        PaymentInfo payTokenPaymentInfo = openPaymentCoreService.getPayTokenPaymentInfo(payToken);
        if (payTokenPaymentInfo == null) {
            return ApiResponse.error("支付令牌error");
        }
        // 3.根据渠道id 查询对应支付渠道
        PaymentChannel paymentChannel = paymentChannelService.getByChannelIdInfo(channelId);
        if (paymentChannel == null) {
            return ApiResponse.error("渠道错误或者是渠道已经关闭!");
        }
        // 4.从ioc容器 找到对应的bean数据 调用方法
        String payBeanId = paymentChannel.getPayBeanId();
        PayStrategy payStrategy = SpringUtil.getBean(payBeanId, PayStrategy.class);
        String toPayHtml = payStrategy.toPay(payTokenPaymentInfo, paymentChannel);
        log.info("<toPayHtml:{}>", toPayHtml);
        return ApiResponse.success(toPayHtml);
    }

}
